<template>
	<ul class="list" ref="list">
		<li 
			v-for="(item,index) in letters" 
			:key="index" 
			:ref="item"
			@click="handLetterClick"
			@touchstart.prevent="handleTouchStart"
			@touchmove="handleTouchMove"
			@touchend="handleTouchEnd"
		>
			{{item}}
		</li>
	</ul>
</template>	
<script>
	export default{
		props:{
			citys:Object
		},
		computed:{
			letters(){
				const letters=[];
				for(let i in this.citys){
					letters.push(i);
				}
				return letters;
			}
		},
		data(){
			return{
				touchStatus:false,
				startY:0,
				lt:0,
				timer:null
			}
		},
		updated(){
			this.startY=this.$refs['A'][0].offsetTop;
			this.lt=this.$refs['list'].offsetTop;
		},
		methods:{
			handLetterClick(e){
				this.$emit('change',e.target.innerText);
			},
			handleTouchStart(){
				this.touchStatus=true
			},
			handleTouchMove(e){
				if(this.touchStatus){
					if(this.timer){
						clearTimeout(this.timer)
					}
					this.timer=setTimeout(()=>{
						const touchY=e.touches[0].clientY-this.lt;
						const index=Math.floor((touchY-this.startY)/this.$refs['A'][0].offsetHeight);
						if(index>=0 && index<this.letters.length){
							this.$emit('change',this.letters[index]);
						}
					},16)
					
				}
			},
			handleTouchEnd(){
				this.touchStatus=false
			}
		}
	}
</script>
<style scoped lang="stylus">	
	@import '~style/varibles.styl';
	.list
		position	absolute
		top	1.76rem
		bottom	0
		right	0
		width	0.6rem
		color	#333
		display	flex
		flex-direction	column
		justify-content	center
		text-align	center
		font-family	Arial
		li
			font-size	0.3rem
			line-height	0.4rem
			color	$bgColor
</style>